package mobi.greenplate.theweb.resource;

import java.net.URI;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import mobi.greenplate.theweb.converter.OfferRideConverter;
import mobi.greenplate.theweb.converter.OfferRideListConverter;
import mobi.greenplate.theweb.domain.OfferRide;
import mobi.greenplate.theweb.repository.OfferRideRepository;
import mobi.greenplate.theweb.user.UserUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;


@Path("/offerRide")
@Component
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class OfferRideResource {

    @Inject
    private OfferRideRepository offerRideRepository;
    @Context
    private UriInfo uriInfo;

    @GET
    @Produces({"application/xml", "application/json"})
    public OfferRideListConverter getOfferRides(@QueryParam("first") @DefaultValue("0") int first,
            @QueryParam("max") @DefaultValue("10") int max) {
        final List<OfferRide> offerRideList = offerRideRepository.findAll(first, max);
        final OfferRideListConverter offerRideListConverter = new OfferRideListConverter(offerRideList);
        return offerRideListConverter;
    }

    @GET
    @Produces({"application/xml", "application/json"})
    @Path("{id}")
    public OfferRideConverter getOfferRideById(@PathParam("id") Long id) {
        final OfferRide offerRide = offerRideRepository.findById(id);
        final OfferRideConverter offerRideConverter = new OfferRideConverter(offerRide);
        return offerRideConverter;
    }

    @POST
    @Consumes({"application/xml", "application/json"})
    public Response createOfferRide(OfferRideConverter offerRideConverter) {
        final OfferRide forSave = offerRideConverter.getEntity();
        forSave.setId(null); //makes sure it is allways new..
        final OfferRide offerRide = offerRideRepository.save(forSave,UserUtils.currentUser());
        final URI uri = uriInfo.getAbsolutePathBuilder().
                path(offerRide.getId().toString()).
                build();
        return Response.created(uri).build();
    }

    @PUT
    @Consumes({"application/xml", "application/json"})
    @Path("{id}")
    public OfferRideConverter updateOrCreateOfferRide(@PathParam("id") Long id, OfferRideConverter offerRideConverter) {
       //if the resource doesn't exist.. it would be created...with PUT.. could be added additional check ... 
        final OfferRide forSave = offerRideConverter.getEntity();
        forSave.setId(id); //makes sure it is exactly the one that has to be updated
        final OfferRide offerRide = offerRideRepository.save(forSave,UserUtils.currentUser());
        return new OfferRideConverter(offerRide);
    }

    @DELETE
    @Path("{id}")
    public void deleteOfferRide(@PathParam("id") Long id) {
       offerRideRepository.delete(id);
    }
}

